package com.wy.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.wy.model.Gathering;
import com.wy.repository.GatheringRepository;
import com.wy.service.GatheringService;
import com.wy.utils.IdWorker;

@Service
public class GatheringServiceImpl implements GatheringService {

	@Autowired
	private GatheringRepository gatheringRepository;

	@Autowired
	private IdWorker idWorker;

	/**
	 * 查询全部列表
	 * 
	 * @return
	 */
	public List<Gathering> findAll() {
		return gatheringRepository.findAll();
	}

	/**
	 * 条件查询+分页
	 * 
	 * @param whereMap
	 * @param page
	 * @param size
	 * @return
	 */
	public Page<Gathering> findSearch(Map<String, Object> whereMap, int page, int size) {
		Specification<Gathering> specification = createSpecification(whereMap);
		PageRequest pageRequest = PageRequest.of(page - 1, size);
		return gatheringRepository.findAll(specification, pageRequest);
	}

	/**
	 * 条件查询
	 * 
	 * @param whereMap
	 * @return
	 */
	public List<Gathering> findSearch(Map<String, Object> whereMap) {
		Specification<Gathering> specification = createSpecification(whereMap);
		return gatheringRepository.findAll(specification);
	}

	/**
	 * 根据ID查询实体
	 * 
	 * @param id
	 * @return
	 * @Cacheable 添加缓存 value属性表示缓存整体唯一标识,key属性标识缓存键值对中的key
	 */
	@Cacheable(value = "gathering", key = "#id")
	public Gathering findById(String id) {
		return gatheringRepository.findById(id).get();
	}

	/**
	 * 增加
	 * 
	 * @param gathering
	 */
	public void add(Gathering gathering) {
		gathering.setId(idWorker.nextId() + "");
		gatheringRepository.save(gathering);
	}

	/**
	 * 修改
	 * 
	 * @param gathering
	 */
	@CacheEvict(value = "gathering", key = "#gathering.id")
	public void update(Gathering gathering) {
		gatheringRepository.save(gathering);
	}

	/**
	 * 删除
	 * 
	 * @param id
	 */
	@CacheEvict(value = "gathering", key = "#gathering.id")
	public void deleteById(String id) {
		gatheringRepository.deleteById(id);
	}

	/**
	 * 动态条件构建
	 * 
	 * @param searchMap
	 * @return
	 */
	private Specification<Gathering> createSpecification(Map<String, Object> searchMap) {

		return new Specification<Gathering>() {

			private static final long serialVersionUID = -4843432592357756742L;

			@Override
			public Predicate toPredicate(Root<Gathering> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicateList = new ArrayList<Predicate>();
				// 编号
				if (searchMap.get("id") != null && !"".equals(searchMap.get("id"))) {
					predicateList
							.add(cb.like(root.get("id").as(String.class), "%" + (String) searchMap.get("id") + "%"));
				}
				// 活动名称
				if (searchMap.get("name") != null && !"".equals(searchMap.get("name"))) {
					predicateList.add(
							cb.like(root.get("name").as(String.class), "%" + (String) searchMap.get("name") + "%"));
				}
				// 大会简介
				if (searchMap.get("summary") != null && !"".equals(searchMap.get("summary"))) {
					predicateList.add(cb.like(root.get("summary").as(String.class),
							"%" + (String) searchMap.get("summary") + "%"));
				}
				// 详细说明
				if (searchMap.get("detail") != null && !"".equals(searchMap.get("detail"))) {
					predicateList.add(
							cb.like(root.get("detail").as(String.class), "%" + (String) searchMap.get("detail") + "%"));
				}
				// 主办方
				if (searchMap.get("sponsor") != null && !"".equals(searchMap.get("sponsor"))) {
					predicateList.add(cb.like(root.get("sponsor").as(String.class),
							"%" + (String) searchMap.get("sponsor") + "%"));
				}
				// 活动图片
				if (searchMap.get("image") != null && !"".equals(searchMap.get("image"))) {
					predicateList.add(
							cb.like(root.get("image").as(String.class), "%" + (String) searchMap.get("image") + "%"));
				}
				// 举办地点
				if (searchMap.get("address") != null && !"".equals(searchMap.get("address"))) {
					predicateList.add(cb.like(root.get("address").as(String.class),
							"%" + (String) searchMap.get("address") + "%"));
				}
				// 是否可见
				if (searchMap.get("state") != null && !"".equals(searchMap.get("state"))) {
					predicateList.add(
							cb.like(root.get("state").as(String.class), "%" + (String) searchMap.get("state") + "%"));
				}
				// 城市
				if (searchMap.get("city") != null && !"".equals(searchMap.get("city"))) {
					predicateList.add(
							cb.like(root.get("city").as(String.class), "%" + (String) searchMap.get("city") + "%"));
				}
				return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));
			}
		};
	}
}